//============================================================================
//
// parseArgs.js
// --------------
//
// Parse and handle command-line arguments. Nothing in here actually "does"
// anything. Methods from the cli.js module are called, this is just the
// "router" that gets us to them.
// 
//============================================================================

var	path = require('path');
var saCore = require('../core/saCore.js');
var saCLI = require("./saCLI.js");

//----------------------------------------------------------------------------
// 

module.exports = {

parseArgs: function(args) {

	var myname = path.basename(args[1])
		myargs = args.slice(2);

	switch(myargs[0]) {

	//------------------------------------------------------------------------

	case 'group':
		
		// Every command except 'list' and 'help' requires a group name

		if ( ( myargs[1] !== 'list' && ! myargs[2] ) && myargs[1] !==
		'help') {
			saCore.die('no group name.');
		}

		var group = myargs[2];

		switch(myargs[1]) {

			case 'list':
				
				if (myargs[2]) {
					saCore.die('Invalid syntax.');
				}

				saCLI.groupList();
				break;

			case 'count':
				saCLI.groupCount(group);
				break;

			case 'create':
				saCLI.groupCreate([ group, myargs[3] ]);
				break;

			case 'comment':

				// Check we've been given a comment

				if (!myargs[3]) {
					saCore.die('Group name and comment string are required.');
				}

				saCLI.groupComment(group, myargs[3]);
				break;

			case 'members': // fall-through is deliberate
			case 'show':
				saCLI.groupShow(group);
				break;

			case 'delete': 
				saCLI.groupDelete(group);
				break;

			case 'rename':

				// check we have a new group name

				if (!myargs[3]) {
					saCore.die('Group name and new group name are required.');
				}

				saCLI.groupRename(group, myargs[3]);
				break;

			case 'export':
				saCLI.groupExport(group);
				break;

			case 'import':

				if (!myargs[3]) {
					saCore.die('Group name and filename are required.');
				}

				saCLI.groupImport(group, myargs[3]);
				break;
				
			default:

				// Print usage information

				console.log(myname + ' group'
				+ '\n  list' 
				+ '\n  create <group> [comment]' 
				+ '\n  rename <group> <new_name>' 
				+ '\n  comment <group> <comment>'
				+ '\n  show <group>'
				+ '\n  count <group>'
				+ '\n  delete <group>'
				+ '\n  import <group> <filename>'
				+ '\n  export <group>'
				);

		}
		break;

	//------------------------------------------------------------------------

	case 'host':
		
		var path_args,	// temporary array used to split up group/host arg
			group,	// the group
			host;	// the host

		// Host related operations. Most things use the group/hosts syntax,
		// so let's break those off now

		if (myargs[1] !== 'help' && myargs[1] !== 'list') {
		
			if (! myargs[2]) {
				saCore.die('Insufficient arguments.');
			}

			if (myargs[1] === 'import') {
				group = myargs[2];
			}
			else {

				path_args = myargs[2].split('/');

				// We need a group and host for everything except import

				if (path_args.length !== 2 ) {
					saCore.die("Hosts must be defined as 'group/hostname'.");
				}
			
				group = path_args[0];
				host = path_args[1];
			}

		}
		
		switch(myargs[1]) {

			case 'list':
				
				if (myargs[2]) {
					saCore.die('Invalid syntax.');
				}
				saCLI.hostList();
				break;

			case 'checkpoints':	
				saCLI.hostCheckpoints(group, host);
				break;

			case 'delete':
				saCLI.hostDelete(group, host);
				break;

			case 'move':

				if (!myargs[3]) {
					saCore.die('Insufficient arguments.');
				}

				saCLI.hostMove(group, host, myargs[3]);
				break;

			case 'zones':
				saCLI.hostZones(group, host);
				break;

			case 'show':
				saCore.die('Show host is not implemented yet');
				break;

			case 'import':

				if (!myargs[3]) {
					saCore.die('Group name and filename are required.');
				}

				saCLI.hostImport(group, myargs[3]);
				break;

			case 'export':
				saCore.die('Exporting hosts is not implemented yet');
				break;

			default:
			
				// Print usage information

				console.log(myname + ' host'
				+ '\n  list'
				+ '\n  checkpoints <group/host>'
				+ '\n  delete <group/host>'
				+ '\n  move <group/host> <group>'
				+ '\n  zones <group/host>'
				+ '\n  import <group> <filename>'
				+ '\n  export <group/host>'
				+ '\n  show <group/host>'
				);

		}
		break;

	//------------------------------------------------------------------------
	
	case 'zone':
		
		switch(myargs[1]) {

			case 'parent':
				saCLI.zoneParent(myargs[2]);
				break;

			case 'delete':
				saCLI.zoneDelete(group, host, zone);
				break;

			case 'show':
				saCore.die('zone show is not implemented yet.');
				break;

			default:
				console.log(myname + ' zone'
				+ '\n  parent <zone>'
				+ '\n  delete <group/host/zone>'
				+ '\n  show <group/host/zone>'
				);
		}
		break;

	//------------------------------------------------------------------------

	case 'search':

		switch(myargs[1]) {

			case 'latest':
				saCore.die('latest search is not implemented yet.');
				break;

			case 'oldest':
				saCore.die('oldest search is not implemented yet.');
				break;

			default:
				console.log(myname + ' search'
				+ '\n latest <class/property>'
				+ '\n oldest <class/property>'
				);

		}
		break;

	//------------------------------------------------------------------------

	case 'version':

		// Print the core version and exit

		console.log(saCore.getVersion());
		process.exit(0);
		break;

	//------------------------------------------------------------------------

	case 'help':
		console.log('Valid subcommands are: '
		+ 'group, host, zone, search, info, version, help');
		break;

	//------------------------------------------------------------------------

	case 'info':
		saCLI.info();
		break;

	//------------------------------------------------------------------------

	default:
		console.log('Missing command. For more info run: ' + myname
		+ ' help');

	}

} // close parseArgs

} // close module.exports

